package x.trident.core.micro.gateway.modular.validate.impl;

import cn.hutool.core.util.StrUtil;
import x.trident.core.micro.gateway.modular.validate.BaseValidateService;
import x.trident.core.micro.gateway.modular.validate.TokenService;
import x.trident.core.auth.api.exception.AuthException;
import x.trident.core.auth.api.exception.enums.AuthExceptionEnum;
import x.trident.core.scanner.api.pojo.resource.ResourceDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 纯token验证鉴权服
 *
 * @author Seven
 * @date 2018-08-13 21:52
 */
@Service
@Slf4j
public class TokenValidateService extends BaseValidateService {

    @Resource
    private TokenService tokenService;

    @Override
    public void validate(String token, ResourceDefinition resourceDefinition) {

        // 校验token
        if (resourceDefinition.getRequiredLoginFlag()) {

            // token为空，返回用户校验失败
            if (StrUtil.isEmpty(token)) {
                throw new AuthException(AuthExceptionEnum.TOKEN_GET_ERROR);
            }

            // 校验token和用户会话信息是否正确
            tokenService.validateToken(token);
        }

    }

}
